package cn.korilweb.demodictionary.entity;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

/**
 * @author ：djh
 * @date ：2021/10/28 15:10
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 8354151990917407881L;

    @Getter
    @Setter
    private String status;
    @Getter
    @Setter
    private String message;
    @Getter
    @Setter
    private String error;
    @Getter
    @Setter
    private String path;
    @Getter
    @Setter
    private Date timestamp;
    @Getter
    @Setter
    private T data;

    public static <T> Result<T> ok(){
        return restResult(null, CommonConstants.SUCCESS,"success");
    }

    public static <T> Result<T> ok(String message){
        return restResult(null, CommonConstants.SUCCESS,message);
    }

    public static <T> Result<T> ok(T data){
        return restResult(data,CommonConstants.SUCCESS,"success");
    }

    public static <T> Result<T> ok(T data,String message){
        return restResult(data,CommonConstants.SUCCESS,message);
    }

    public static <T> Result<T> failed(){
        return restResult(null,CommonConstants.FAIL,"fail");
    }
    public static <T> Result<T> failed(String status, T data, String message) {
        return restResult(data, status, message);
    }

    public static <T> Result<T> failed(String message){
        return restResult(null,CommonConstants.FAIL,message);
    }

    public static <T> Result<T> failed(T data){
        return restResult(data,CommonConstants.FAIL,"fail");
    }

    public static <T> Result<T> failed(T data,String message){
        return restResult(data,CommonConstants.FAIL,message);
    }

    public static <T> Result<T> exception(String status, String message) {
        return restResult(null, status, message);
    }

    private static <T> Result<T> restResult(T data, String status, String message) {
        Result<T> apiResult = new Result<>();
        apiResult.setStatus(status);
        apiResult.setData(data);
        apiResult.setMessage(message);
        apiResult.setTimestamp(new Date());
        return apiResult;
    }
}

